<?php
namespace app\admin\controller\agent;

use think\Db;
class Auth extends \app\admin\controller\Common
{
    protected $type = 0;
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\AgentAuth());
        $list = $query->limit($page, $limit)
                ->append(['addtime_text','agent_user_level','agent_user_name','auth_text','endtime_text','agent_user_title'])
                ->order("id desc")
                ->select();
        $count = $query->count();
   
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            if(empty($data['endtime']) && empty($data['is_endtime'])){
                return self::errorResult("请选择结束时间");
            }
            $data['endtime'] = strtotime($data['endtime']);
            if(!empty($data['is_endtime']) && $data['is_endtime']==1){
               $data['endtime'] = 0;
            }
            $data['auth_id'] = implode(",",$data['auth_id']);
            (new \app\common\model\AgentAuth())->save($data);
            return self::successResult();
        } else {
            $auth = (new \app\common\model\AgentAdminAuth())->select();
            $this->assign('auth',$auth);
            return view();
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            $id = request()->param('id');
        
            $table = new \app\common\model\AgentAuth();
            if(empty($data['endtime']) && empty($data['is_endtime'])){
                return self::errorResult("请选择结束时间");
            }
            $data['endtime'] = strtotime($data['endtime']);
            if(!empty($data['is_endtime']) && $data['is_endtime']==1){
               $data['endtime'] = 0;
            }
            $data['auth_id'] = implode(",",$data['auth_id']);
            $row = $table->allowField(true)->save($data, ['id' => $id]);
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\AgentAuth())->where(['id' => $id])->find();
            $info['auth_id'] = explode(",",$info['auth_id']);
            $this->assign('info',$info);
            $auth = (new \app\common\model\AgentAdminAuth())->select();
            $this->assign('auth',$auth);
   
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\AgentAuth();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }

  

 
}
